<template>
  <!--面包屑-->
    <el-row>
        <div class="breadcrumb">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item to="/">主页</el-breadcrumb-item>
                <el-breadcrumb-item to="/wisdomeat">智慧食堂</el-breadcrumb-item>
                <el-breadcrumb-item>商家管理</el-breadcrumb-item>
                <el-breadcrumb-item>个人信息</el-breadcrumb-item>
            </el-breadcrumb>
        </div>
    </el-row>
  <!--    个人信息表格-->
    <el-row>
        <el-card class="infocard">
            <template #header>
                <h2 align="center">商家个人信息</h2>
            </template>
            <el-form>
                <el-form-item label="商家编号">
                    <el-input v-model="info.bNumber" disabled></el-input>
                </el-form-item>
                <el-form-item label="商家名称">
                    <el-input v-model="info.bName"></el-input>
                </el-form-item>
                <el-form-item label="商家地址">
                    <el-input v-model="info.bAddress"></el-input>
                </el-form-item>
                <el-form-item label="商家电话">
                    <el-input v-model="info.bPhone"></el-input>
                </el-form-item>
                <el-form-item label="营业状态">
                    <el-input v-model="info.bsName" disabled></el-input>
                </el-form-item>
                <el-form-item class="button-group">
                    <el-button type="primary" @click="btnEditClick" style="width: 100px;">修改</el-button>
                    <el-button type="primary" @click="btnSwitchClick" style="width: 100px;">
                        <!--                        根据商家营业状态改变-->
                        {{ this.switch }}
                    </el-button>
                </el-form-item>
            </el-form>
        </el-card>
    </el-row>
</template>

<script>
import axios from "axios";
import {ElMessage} from "element-plus";

export default {
    name: "BusinessInfo",
    data() {
        return {
            businessinfo: {}, //商家信息
            businessstatus: [], //商家状态
            bNumber: '', //商家编号
            // 通过info向后端传值
            info: {
                bNumber: '',
                bName: '',
                bAddress: '',
                bPhone: '',
                bsName: ''
            },
            // 商家状态
            switch: ''
        }
    },
    methods: {
        // 初始化表格,通过商家编号获取信息
        initTable() {
            this.bNumber = sessionStorage.getItem('businessNumber')
            if (this.bNumber == null) {
                ElMessage.error({
                    message: '未找到用户信息，请先登录',
                    duration: 1000
                });
            } else {
                axios({
                    url: 'http://localhost:8888/businessinfo/getBusinessListByBNumber',
                    method: 'get',
                    params: {bNumber: this.bNumber}
                }).then((res) => {
                    this.businessinfo = res.data.data[0];
                    this.setDefault();
                    if (this.businessinfo.bstatus === 1) {
                        this.switch = '歇业'
                    } else if (this.businessinfo.bstatus === 2) {
                        this.switch = '营业'
                    } else if (this.businessinfo.bstatus === 3) {
                        this.switch = '整改中'
                        ElMessage.error({
                            message: '商家整改中，禁止操作',
                            duration: 1000
                        })
                    }
                })
            }
        },
        // 将商家信息赋给info
        setDefault() {
            this.info.bNumber = this.businessinfo.bnumber;
            this.info.bName = this.businessinfo.bname;
            this.info.bAddress = this.businessinfo.baddress;
            this.info.bPhone = this.businessinfo.bphone;
            this.info.bsName = this.businessinfo.bsname;
        },
        // 修改商家信息
        btnEditClick() {
            if (this.bNumber == null) { //判断是否登录
                ElMessage.error({
                    message: '未找到用户信息，请先登录',
                    duration: 1000
                });
            } else if (this.businessinfo.bstatus === 3) { //判断商家状态
                ElMessage.error({
                    message: '商家整改中，禁止操作',
                    duration: 1000
                })
            } else {
                axios({
                    url: 'http://localhost:8888/businessinfo/updateBusiness',
                    method: 'post',
                    params: {
                        bNumber: this.info.bNumber,
                        bName: this.info.bName,
                        bAddress: this.info.bAddress,
                        bPhone: this.info.bPhone
                    }
                }).then((res) => {
                    if (res.data.success) {
                        ElMessage.success({
                            message: '修改成功',
                            duration: 1000
                        })
                        this.initTable();
                    }
                })
            }
        },
        // 修改商家营业状态
        btnSwitchClick() {
            if (this.bNumber == null) { //判断是否登录
                ElMessage.error({
                    message: '未找到用户信息，请先登录',
                    duration: 1000
                });
            } else if (this.businessinfo.bstatus === 3) {
                ElMessage.error({
                    message: '商家整改中，禁止操作',
                    duration: 1000
                })
            } else if (this.businessinfo.bstatus === 1) { //判断商家状态
                axios({
                    url: 'http://localhost:8888/businessinfo/offShelfBusiness',
                    method: 'post',
                    params: {bNumber: this.info.bNumber}
                }).then((res) => {
                    if (res.data.success) {
                        this.initTable();
                    }
                })
            } else if (this.businessinfo.bstatus === 2) {
                axios({
                    url: 'http://localhost:8888/businessinfo/onSaleBusiness',
                    method: 'post',
                    params: {bNumber: this.info.bNumber}
                }).then((res) => {
                    if (res.data.success) {
                        this.initTable();
                    }
                })
            }
        }
    },
    mounted() {
        this.initTable();
    }
}
</script>

<style scoped>
.infocard {
    width: 800px;
    margin-left: 350px;
}

.breadcrumb {
    padding: 10px;
}

.button-group {
    margin-left: 270px;
    margin-top: 20px;
}
</style>